#include "ConstraintDb.h"

namespace common {

ConstraintDb::ConstraintDb(PointDb* pointDb, ParameterDb* paramDb) :
	DataBase<Constraint*> ("ConstraintDb"), pointDb(pointDb), paramDb(paramDb) {

}

void ConstraintDb::remove(int id) throw (MyException){
	Constraint* con = get(id);
	if(con->containsParameter()){
		paramDb->remove(con->getParameter()->getId());
	}
	DataBase<Constraint*>::remove(id);
}

ConnectPoints* ConstraintDb::addConnectPoints(int k,int l){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	ConnectPoints* con = new ConnectPoints(getMaxId()+1,pk,pl);
	return dynamic_cast<ConnectPoints*>(add(con));
}
FixPoint* ConstraintDb::addFixPoint(int k){
	Point* pk = pointDb->get(k);

	FixPoint* con = new FixPoint(getMaxId()+1,pk,new Vector(pk->getAnchor()));
	return dynamic_cast<FixPoint*>(add(con));
}
Perpendicular* ConstraintDb::addPerpendicular(int k,int l, int m,int n){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Point* pm = pointDb->get(m);
	Point* pn = pointDb->get(n);
	Perpendicular* con = new Perpendicular(getMaxId()+1,pk,pl,pm,pn);
	return dynamic_cast<Perpendicular*>(add(con));

}
Horizontal* ConstraintDb::addHorizontal(int k,int l){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Horizontal* con = new Horizontal(getMaxId()+1,pk,pl);
	return dynamic_cast<Horizontal*>(add(con));
}
Vertical* ConstraintDb::addVertical(int k,int l){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Vertical* con = new Vertical(getMaxId()+1,pk,pl);
	return dynamic_cast<Vertical*>(add(con));
}
Parallel* ConstraintDb::addParallel(int k,int l,int m,int n){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Point* pm = pointDb->get(m);
	Point* pn = pointDb->get(n);
	Parallel* con = new Parallel(getMaxId()+1,pk,pl,pm,pn);
	return dynamic_cast<Parallel*>(add(con));
}
SameLength* ConstraintDb::addSameLength(int k,int l,int m,int n){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Point* pm = pointDb->get(m);
	Point* pn = pointDb->get(n);
	SameLength* con = new SameLength(getMaxId()+1,pk,pl,pm,pn);
	return dynamic_cast<SameLength*>(add(con));
}
Distance* ConstraintDb::addDistanec(int k,int l,skreal paramValue){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Parameter* par = paramDb->addParameter(paramValue);

	Distance* con = new Distance(getMaxId()+1,pk,pl,par);
	return dynamic_cast<Distance*>(add(con));
}
Angle* ConstraintDb::addAngle(int k,int l,int m,int n,skreal paramValue){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Point* pm = pointDb->get(m);
	Point* pn = pointDb->get(n);
	Parameter* par = paramDb->addParameter(paramValue);
	Angle* con = new Angle(getMaxId()+1,pk,pl,pm,pn,par);
	return dynamic_cast<Angle*>(add(con));
}
Tangency* ConstraintDb::addTangency(int k,int l,int m,int n){
	Point* pk = pointDb->get(k);
	Point* pl = pointDb->get(l);
	Point* pm = pointDb->get(m);
	Point* pn = pointDb->get(n);
	Tangency* con = new Tangency(getMaxId()+1,pk,pl,pm,pn);
	return dynamic_cast<Tangency*>(add(con));
}
ConstraintDb::~ConstraintDb() {

}

}
